package com.controller;


import com.comment.CommentResult;
import com.domain.entity.Address;
import com.domain.req.AddAddressReq;
import com.domain.req.AddressUpdateReq;
import com.domain.req.PageReq;
import com.github.pagehelper.PageInfo;
import com.service.AddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin//跨越
@RestController
@Slf4j
@RequestMapping("/address")
public class AddressController {
    @Resource
    private AddressService addressService;


    /**
     * 新增收货地址
     *
     * @param addAddressReq 新增地址请求
     */
    @PostMapping("/addAddress")
    public CommentResult<Void> addAddress(@RequestBody AddAddressReq addAddressReq) {
        addressService.addAddress(addAddressReq);
        return CommentResult.success();
    }

    @PutMapping("/update")
    public CommentResult<Void> update(@RequestBody AddressUpdateReq addressUpdateReq) {
        addressService.update(addressUpdateReq);
        return CommentResult.success();
    }

    /**
     * 分页查询本人的收货地址
     */
    @GetMapping("/pageList")
    public CommentResult<PageInfo<Address>> listAddresses(PageReq pageReq, Address address) {
        PageInfo<Address> addressPageInfo = addressService.listAddresses(pageReq, address);
        return CommentResult.success(addressPageInfo);
    }

    @GetMapping("/list")
    public CommentResult<List<Address>> list() {
        List<Address> list = addressService.list();
        return CommentResult.success(list);
    }


    /**
     * 根据收货地址id删除本人的收货地址
     */
    @DeleteMapping("/delete/{id}")
    public CommentResult<Void> delete(@PathVariable("id") Integer id) {
        addressService.delete(id);
        return CommentResult.success();
    }

    /**
     * 清空自己的收货地址
     */
    @DeleteMapping("/deleteAll")
    public CommentResult<Void> deleteAll() {
        addressService.deleteAll();
        return CommentResult.success();
    }

    /**
     * 根据id查询地址信息
     */
    @GetMapping("/getById/{id}")
    public CommentResult<Address> getById(@PathVariable("id") Integer id) {
        Address address = addressService.getById(id);
        return CommentResult.success(address);
    }

    //添加
//    @RequestMapping(value = "addAddress")
//    public CommentResult<Address> addAddress(Address address) {
//        log.info("-----------addAddress--------------");
//        addressMapper.insertAddress(address);
//        return CommentResult.success(address);
//    }

    //查询
//    @RequestMapping(value = "queryaddress")
//    public CommentResult<PageInfo<Address>> queryaddress(Address address, int pageNum, int pageSize) {
//        log.info("-----------queryaddress--------------");
//        System.out.println("pageNum   " + pageNum);
//        System.out.println("pageSize   " + pageSize);
//        //初始化
//        PageHelper.startPage(pageNum, pageSize);
//        //原查询
//        List<Address> addressList = addressMapper.selectAddress();
//        //格式转换
//        PageInfo<Address> addressPageInfo = new PageInfo<>(addressList);
//        return CommentResult.success(addressPageInfo);
//    }
//
//    //删除
//    @RequestMapping(value = "delAddress")
//    public CommentResult<Void> delAddress(Address address) {
//        log.info("-----------delAddress--------------");
//        addressMapper.delAddress(address);
//        return CommentResult.success();
//    }
//
//    //根据id修改信息
//    @RequestMapping(value = "updateAddress", method = RequestMethod.PUT)
//    public CommentResult<Void> updateAddress(@RequestBody Address address) {
//        log.info(address.toString());
//        // 补充修改时间
//        address.setUpdateTime(new Date());
//        addressMapper.updateAddress(address);
//        return CommentResult.success();
//    }
}
